package com.yf.train.modules.train.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.base.api.api.ApiRest;
import com.yf.base.api.api.controller.BaseController;
import com.yf.base.api.api.dto.BaseIdReqDTO;
import com.yf.base.api.api.dto.BaseIdRespDTO;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.train.modules.train.dto.RepoTrainDTO;
import com.yf.train.modules.train.dto.RepoTrainQuDTO;
import com.yf.train.modules.train.dto.ext.TrainQuDetailDTO;
import com.yf.train.modules.train.dto.request.RepoTrainReqDTO;
import com.yf.train.modules.train.service.RepoTrainQuService;
import com.yf.train.modules.train.service.RepoTrainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* <p>
* 题库训练控制器
* </p>
*
* @author 聪明笨狗
* @since 2022-03-01 18:42
*/
@Api(tags={"题库训练"})
@RestController
@RequestMapping("/api/repo/train")
public class RepoTrainController extends BaseController {

    @Autowired
    private RepoTrainService baseService;

    @Autowired
    private RepoTrainQuService repoTrainQuService;


    /**
     * 查找详情
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找详情")
    @PostMapping("/detail")
    public ApiRest<RepoTrainDTO> detail(@RequestBody BaseIdReqDTO reqDTO) {
        RepoTrainDTO dto = baseService.detail(reqDTO.getId());
        return super.success(dto);
    }


    /**
     * 查找我的训练
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "训练记录分页")
    @PostMapping("/paging")
    public ApiRest<IPage<RepoTrainDTO>> paging(@RequestBody PagingReqDTO<RepoTrainReqDTO> reqDTO) {

        //分页查询并转换
        IPage<RepoTrainDTO> page = baseService.paging(reqDTO);

        return super.success(page);
    }

    /**
     * 开始训练
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "检查是否存在训练")
    @PostMapping("/check")
    public ApiRest<RepoTrainDTO> hasTrain(@RequestBody RepoTrainDTO reqDTO) {
        //转换并返回
        RepoTrainDTO respDTO = baseService.findProcess(reqDTO.getRepoId());
        return super.success(respDTO);
    }

    /**
     * 开始训练
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "开始训练")
    @PostMapping("/start")
    public ApiRest<BaseIdRespDTO> startTrain(@RequestBody RepoTrainDTO reqDTO) {
        // 转换并返回
        String trainId = baseService.start(reqDTO);
        return super.success(new BaseIdRespDTO(trainId));
    }

    /**
     * 开始训练
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "填充答案")
    @PostMapping("/fill")
    public ApiRest<RepoTrainQuDTO> fillResult(@RequestBody TrainQuDetailDTO reqDTO) {
        //转换并返回
        RepoTrainQuDTO respDTO = baseService.fill(reqDTO);
        return super.success(respDTO);
    }

    /**
     * 完成训练
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "完成训练")
    @PostMapping("/finish")
    public ApiRest finish(@RequestBody RepoTrainDTO reqDTO) {
        //转换并返回
        baseService.finish(reqDTO.getId());
        return super.success();
    }

    /**
     * 查找训练答题卡
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找训练答题卡", notes = "id为训练id")
    @PostMapping("/card")
    public ApiRest<List<RepoTrainQuDTO>> card(@RequestBody BaseIdReqDTO reqDTO) {
        //转换并返回
        List<RepoTrainQuDTO> list = repoTrainQuService.listCard(reqDTO.getId());
        return super.success(list);
    }

    /**
     * 查找题目详情
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找题目详情")
    @PostMapping("/qu-detail")
    public ApiRest<TrainQuDetailDTO> trainQuDetail(@RequestBody RepoTrainQuDTO reqDTO) {
        //转换并返回
        TrainQuDetailDTO detail = repoTrainQuService.detail(reqDTO.getTrainId(), reqDTO.getMode(), reqDTO.getQuId());
        return super.success(detail);
    }
}
